﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web;

namespace SMBL
{
    namespace Global
    {
        /// <summary>
        /// Error Report
        /// </summary>
        class ErrorReport
        {
            // HTML Elements
            private String Html = "<html><head><title>Error Report</title></head>";
            private String HtmlEnd = "</html>";
            private String Body = "<body>";
            private String BodyEnd = "</body>";
            private String ReportTitle = "<h1>Error Report</h1>";
            private String ReportListTitle = "<h1>Report List</h1>";
            private String EndLine = "<br />";
            private String WbInfo = "";

            // Report Body
            private String ReportBody;

            /// <summary>
            /// The construction
            /// Generate the report body
            /// </summary>
            /// <param name="exp"></param>
            public ErrorReport(Exception exp)
            {
                ReportBody = ReportTitle + EndLine + WbInfo +
                    "<h3>Date:</h3> " + DateTime.Now.ToString() + EndLine +
                    "<h3>The Method:</h3> " + exp.TargetSite + EndLine +
                    "<h3>Exception Type:</h3> " + exp.GetType().FullName + EndLine +
                    "<h3>Message:</h3> " + exp.Message + EndLine +
                    "<h3>Source:</h3> " + exp.Source + EndLine +
                    "<h3>StackTrace:</h3> ";
                Int32 atStart = 0;
                Int32 atNextStart = 0;
                while (true)
                {
                    //atStart = exp.StackTrace.IndexOf("at", atNextStart);
                    atNextStart = exp.StackTrace.IndexOf("at ", atStart + 1);

                    // End of the string
                    if (atNextStart <= 0)
                    {
                        ReportBody += (exp.StackTrace.Substring(atStart, exp.StackTrace.Length - atStart) + EndLine);
                        break;
                    }

                    ReportBody += (exp.StackTrace.Substring(atStart, atNextStart - atStart) + EndLine);
                    atStart = atNextStart;
                }
            }

            /// <summary>
            /// The construction
            /// Generate the report body
            /// </summary>
            /// <param name="errList"></param>
            public ErrorReport(ArrayList errList)
            {
                ReportBody = ReportListTitle + EndLine + WbInfo +
                    "<h3>Date:</h3> " + DateTime.Now.ToString() + EndLine +
                    "<h3>Description:</h3> " + EndLine +
                    "This report is generate by ErrorReport according to an ArrayList which was used to " +
                    "store temp errors." + EndLine +
                    "<h3>Errors</h3>";
                for (int i = errList.Count - 1; i >= 0; --i)
                {
                    // All format is controlled by the other error system
                    ReportBody += errList[i].ToString();
                }
            }

            private void getWebSiteInfo()
            {
                WbInfo += "<h2>Website Name:</h2>";
                WbInfo += WebSiteInfo.Ins().WebSiteName;
                WbInfo += EndLine;

                WbInfo += "<h2>Website Owner:</h2>";
                WbInfo += WebSiteInfo.Ins().WebSiteOwner;
                WbInfo += EndLine;

                WbInfo += "<h2>Website URL:</h2>";
                WbInfo += WebSiteInfo.Ins().WebSiteUrl;
                WbInfo += EndLine;

                WbInfo += "<h2>Website Request URL:</h2>";
                WbInfo += HttpContext.Current.Request.Url.ToString();
                WbInfo += EndLine;

                WbInfo += "<h2>User Platform:</h2>";
                WbInfo += HttpContext.Current.Request.Browser.Platform;
                WbInfo += EndLine;

                WbInfo += "<h2>Browser:</h2>";
                WbInfo += HttpContext.Current.Request.Browser.Browser;
                WbInfo += EndLine;

                WbInfo += "<h2>Browser:</h2>";
                WbInfo += HttpContext.Current.Request.Browser.Version;
                WbInfo += EndLine;

                WbInfo += EndLine;
            }
            // Get the full Report Body
            public override String ToString()
            {
                return (Html + Body + ReportBody + BodyEnd + HtmlEnd);
            }
        }
    }
}